package com.tnovoselec.android.feed.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.tnovoselec.android.favorites.entity.ForecastFavoritesDto;
import com.tnovoselec.android.favorites.entity.MeteoFavoritesDto;


public class WeatherDataParser extends DefaultHandler {

    private StringBuilder buffer;
    // private WeatherDto weather;
    // private Forecast tempForecast;

    private MeteoFavoritesDto weather;
    private ForecastFavoritesDto tempForecast;

    private final static String TAG_CURRENT_CONDITIONS = "current_conditions";
    private final static String TAG_CONDITION = "condition";
    private final static String TAG_TEMP_C = "temp_c";
    private final static String TAG_HUMIDITY = "humidity";
    private final static String TAG_ICON = "icon";
    private final static String TAG_WIND_DIRECTION = "wind_direction";
    private final static String TAG_WIND_SPEED = "wind_speed";
    private final static String TAG_BACKGROUND = "background";
    private final static String TAG_FORECAST_CONDITIONS = "forecast_conditions";
    private final static String TAG_DAY_OF_WEEK = "day_of_week";
    private final static String TAG_LOW = "low";
    private final static String TAG_HIGH = "high";

    private final static String ATTR_DATA = "data";
    private final static String ATTR_UNIT = "unit";

    private boolean inCurrentConditions = false;
    private boolean inForecastConditions = false;

    public WeatherDataParser(MeteoFavoritesDto weather) {
	super();
	this.weather = weather;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
	buffer = new StringBuilder("");

	if (localName.equalsIgnoreCase(TAG_CURRENT_CONDITIONS)) {
	    inCurrentConditions = true;
	} else if (localName.equalsIgnoreCase(TAG_CONDITION)) {
	    if (inCurrentConditions) {
		weather.setConditionString(attributes.getValue(ATTR_DATA));
	    } else if (inForecastConditions) {
		tempForecast.setConditionString(attributes.getValue(ATTR_DATA));
	    }
	} else if (localName.equalsIgnoreCase(TAG_TEMP_C)) {
	    try {
		weather.setTemperature(Integer.parseInt(attributes.getValue(ATTR_DATA)));
	    } catch (Exception e) {
		weather.setTemperature(0);
	    }
	} else if (localName.equalsIgnoreCase(TAG_HUMIDITY)) {
	    weather.setHumidity(attributes.getValue(ATTR_DATA));
	} else if (localName.equalsIgnoreCase(TAG_ICON)) {
	    if (inCurrentConditions) {
		weather.setIconUrl(attributes.getValue(ATTR_DATA));
	    } else if (inForecastConditions) {
		tempForecast.setIconUrl(attributes.getValue(ATTR_DATA));
	    }
	} else if (localName.equalsIgnoreCase(TAG_WIND_DIRECTION)) {
	    weather.setWindDirection(attributes.getValue(ATTR_DATA));
	} else if (localName.equalsIgnoreCase(TAG_BACKGROUND)) {
	    weather.setBackgroundImageUrl(attributes.getValue(ATTR_DATA));
	} else if (localName.equalsIgnoreCase(TAG_WIND_SPEED)) {
	    try {
		weather.setWindSpeed(Integer.parseInt(attributes.getValue(ATTR_DATA)));
	    } catch (Exception e) {
		weather.setWindSpeed(0);
	    }
	    weather.setWindSpeedUnit(attributes.getValue(ATTR_UNIT));
	} else if (localName.equalsIgnoreCase(TAG_FORECAST_CONDITIONS)) {
	    inForecastConditions = true;
	    tempForecast = new ForecastFavoritesDto();
	} else if (localName.equalsIgnoreCase(TAG_DAY_OF_WEEK)) {
	    tempForecast.setDayOfWeekString(attributes.getValue(ATTR_DATA));
	} else if (localName.equalsIgnoreCase(TAG_LOW)) {
	    try {
		tempForecast.setTemperatureLow(Integer.parseInt(attributes.getValue(ATTR_DATA)));
	    } catch (Exception e) {
		tempForecast.setTemperatureLow(0);
	    }
	} else if (localName.equalsIgnoreCase(TAG_HIGH)) {
	    try {
		tempForecast.setTemperatureHigh(Integer.parseInt(attributes.getValue(ATTR_DATA)));
	    } catch (Exception e) {
		tempForecast.setTemperatureHigh(0);
	    }
	}
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
	if (localName.equalsIgnoreCase(TAG_CURRENT_CONDITIONS)) {
	    inCurrentConditions = false;
	} else if (localName.equalsIgnoreCase(TAG_FORECAST_CONDITIONS)) {
	    inForecastConditions = false;
	    tempForecast.setCityId(weather.getCityId());
	    weather.addForecast(tempForecast);
	}
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
	buffer.append(String.valueOf(ch, start, length));
    }

    /**
     * Method used to get weather data from one location
     * 
     * @return weather data for specific location
     */
    public MeteoFavoritesDto getWeather() {
	return weather;
    }

}
